package main

import (
	_ "github.com/go-sql-driver/mysql"
	_ "shopping/routers"
	"github.com/astaxie/beego/orm"
	"github.com/astaxie/beego"
	"fmt"
)

func init() {
	// Mysql 配置
	//注册数据库驱动
	//orm.RegisterDriver("mysql", orm.DRMySQL)
	// 参数1        数据库的别名，用来在 ORM 中切换数据库使用
	// 参数2        driverName
	// 参数3        对应的链接字符串
	// 参数4(可选)  设置最大空闲连接
	// 参数5(可选)  设置最大数据库连接 (go >= 1.2)
	//maxIdle := 30
	//maxConn := 30
	//orm.RegisterDataBase("default", "mysql", "root:Zhl@12345678@/shopping?charset=utf8", maxIdle, maxConn)
	//orm.RegisterDataBase("default", "mysql", "demo:demo2018@tcp(172.17.16.5:3306)/shopping?charset=utf8",maxIdle, maxConn)

	//mysqluser = "root"
	//mysqlpass = "Zhl@12345678"
	//mysqlurls = "127.0.0.1"
	//mysqldb = "shopping"
	//mysqlport = "3306"

	user := beego.AppConfig.String("mysqluser")
	passwd := beego.AppConfig.String("mysqlpass")
	host := beego.AppConfig.String("mysqlurls")
	port, err := beego.AppConfig.Int("mysqlport")
	dbname := beego.AppConfig.String("mysqldb")
	maxIdle, _ := beego.AppConfig.Int("maxIdle")
	maxConn, _ := beego.AppConfig.Int("maxConn")
	if nil != err {
		port = 3306
	}
	if beego.AppConfig.String("runmode") == "dev" {
		orm.Debug = true
	}
	orm.RegisterDriver("mysql", orm.DRMySQL)
	//orm.RegisterDataBase("default", "mysql", "root:@/blog?charset=utf8", 30)
	orm.RegisterDataBase("default", "mysql", fmt.Sprintf("%s:%s@tcp(%s:%d)/%s?charset=utf8", user, passwd, host, port, dbname), maxIdle, maxConn)
	// 自动建表
	orm.RunSyncdb("default", false, true)

}

func main() {
	//swagger的配置
	//if beego.BConfig.RunMode == "dev" {
	//	beego.BConfig.WebConfig.DirectoryIndex = true
	//	beego.BConfig.WebConfig.StaticDir["/swagger"] = "swagger"
	//}
	beego.Run()
}
